#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <Windows.h>
#include"GomoKuNode.h"
#include<math.h>
#include<fstream>
#include<map>
#include<algorithm>
#include<vector>
using namespace std;
SettingsVal mysettingVal = { 1280,720,640,360,50 };
GomokuNode* lines[45][15] = { { nullptr } };
GomokuNode board[169];  // 棋盘节点
CConstVal constVal;

struct ScoreData {
	int id;
	int score;
};

//数组名称第一个数字代表棋子状况，第二个数字标识黑（1）/白（2）棋；同种情况下白棋得分高
//活四
int a4_2[6] = { 0,2,2,2,2,0 };
int a4_21[6] = { 0,1,1,1,1,0 };
int a4_2_score = 9000;
//其余四
int a42[8][6] = {	{1,2,2,2,2,0},{0,2,2,2,2,1},
					{1,2,0,2,2,2},{2,0,2,2,2,1},
					{1,2,2,0,2,2},{2,2,0,2,2,1},
					{1,2,2,2,0,2},{2,2,2,0,2,1} };
int a421[8][6] = { {2,1,1,1,1,0},{0,1,2,2,2,2},
					{2,1,0,1,1,1},{1,0,1,1,1,2},
					{2,1,1,0,1,1},{1,1,0,1,1,2},
					{2,1,1,1,0,1},{1,1,1,0,1,2} };
int a42_score = 8960;
//活三
int a3_2[3][5] = { { 0,2,2,2,0},
					{ 0,2,2,2,2} ,
					{ 2,2,2,2,0} };

int a3_21[3][5] = { { 0,1,1,1,0},
					{ 0,1,1,1,1} ,
					{ 1,1,1,1,0} };
int a3_2_score = 8920;
//其余三
int a32[6][5] = { {1,2,2,2,0},{0,2,2,2,1},
					{1,2,0,2,2},{2,0,2,2,1},
					{1,2,2,0,2},{2,2,0,2,1} };

int a321[6][5] = { {2,1,1,1,0},{0,1,1,1,2},
					{2,1,0,1,1},{1,0,1,1,2},
					{2,1,1,0,1},{1,1,0,1,2} };
int a32_score = 8880;
//活二
int a2_2[4] = { 0,2,2,0 };
int a2_21[4] = { 0,1,1,0 };
int a2_2_score = 8840;
//其余二
int a22[4][4] = { {1,2,2,0},{1,2,0,2},
					{0,2,2,1},{2,0,2,1} };

int a221[4][4] = { {2,1,1,0},{2,1,0,1},
					{0,1,1,2},{1,0,1,2} };
int a22_score = 8800;


//活四
int a4_1[6] = { 0,1,1,1,1,0 };
int a4_11[6] = { 0,2,2,2,2,0 };
int a4_1_score = 8980;
//其余四
int a41[8][6] = {	{2,1,1,1,1,0},{0,1,1,1,1,2},
					{2,1,0,1,1,1},{1,0,1,1,1,2},
					{2,1,1,0,1,1},{1,1,0,1,1,2},
					{2,1,1,1,0,1},{1,1,1,0,1,2} };
int a411[8][6] = { {1,2,2,2,2,0},{0,2,2,2,2,1},{1,2,0,2,2,2},{2,0,2,2,2,1},{1,2,2,0,2,2},{2,2,0,2,2,1},{1,2,2,2,0,2},{2,2,2,0,2,1} };
int a41_score = 8940;
//活三
int a3_1[3][5] = { { 0,1,1,1,0 } ,
					{ 0,1,1,1,1} ,
					{ 1,1,1,1,0} };
int a3_11[3][5] = { { 0,2,2,2,0 } ,
					{ 0,2,2,2,2} ,
					{ 2,2,2,2,0} };
int a3_1_score = 8900;
//其余三
int a31[6][5] = { {2,1,1,1,0},{0,1,1,1,2},
					{2,1,0,1,1},{1,0,1,1,2},
					{2,1,1,0,1},{1,1,0,1,2} };
int a311[6][5] = { {1,2,2,2,0},{0,2,2,2,1},
					{1,2,0,2,2},{2,0,2,2,1},
					{1,2,2,0,2},{2,2,0,2,1} };
int a31_score = 8860;
//活二
int a2_1[4] = { 0,1,1,0 };
int a2_11[4] = { 0,2,2,0 };
int a2_1_score = 8820;
//其余二
int a21[4][4] = { {2,1,1,0},{0,2,2,1},
					{2,1,0,1},{1,0,1,2} };

int a211[4][4] = { {1,2,2,0},{0,1,1,2},
					{1,2,0,2},{2,0,2,1} };
int a21_score = 8780;

int is_equal(int a[], int b[], int n) {
	for (int i = 0; i < n; i++) {
		if (a[i] != b[i])
			return 0;
	}
	return 1;
}

void set_basic_score() {
	//看xyz坐标，异号的点来决定其所在的六边形，以此赋分
	for (int i = 0; i < 169; i++) {
		int index_x = board[i].Line_x;
		int index_y = board[i].Line_y;
		int index_z = board[i].Line_z;
		//先给坐标轴上的点赋值
		if (index_x == 0) {
			board[i].score = 21 - abs(index_y) * 3;
		}
		else if (index_y == 0) {
			board[i].score = 21 - abs(index_z) * 3;
		}
		else if (index_z == 0) {
			board[i].score = 21 - abs(index_x) * 3;
		}
		//对于其他点：相乘大于0，即为同号
		if (index_x * index_y > 0) {
			board[i].score = 21 - abs(index_z) * 3;
		}
		else if (index_y * index_z > 0) {
			board[i].score = 21 - abs(index_x) * 3;
		}
		else if (index_z * index_x > 0) {
			board[i].score = 21 - abs(index_y) * 3;
		}
	}
}


void initialize_board_and_lines() {
	// Initialize the board
	int id = 0;
	for (int x = -7; x <= 7; ++x) {
		for (int y = -7; y <= 7; ++y) {
			int z = -x - y;
			if (z >= -7 && z <= 7) {
				board[id] = GomokuNode(id, x, y, z);
				id++;
			}
		}
	}

	int line_indices[45] = { 0 };
	for (int i = 0; i < 169; ++i) {
		int line_x_index = board[i].Line_x + 7;
		int line_y_index = board[i].Line_y + 7 + 15;
		int line_z_index = board[i].Line_z + 7 + 30;
		lines[line_x_index][line_indices[line_x_index]] = &board[i];  // x-lines start at index 0
		line_indices[line_x_index]++;
		lines[line_y_index][line_indices[line_y_index]] = &board[i];  // y-lines start at index 15
		line_indices[line_y_index]++;
		lines[line_z_index][line_indices[line_z_index]] = &board[i];  // z-lines start at index 30
		line_indices[line_z_index]++;
	}

}

void drawBoard()
{
	int x = mysettingVal.x / 2, y = mysettingVal.y / 2;  // 中心点的坐标
	int r = mysettingVal.sideLength;  // 中心六边形的半径

	putpixel(x, y, WHITE);

	setlinestyle(PS_SOLID, 1);       //设置直线为实线，宽度为1 px
	// 绘制7个同心六边形
	CPoint hexagon[6];  //存放六边形的每个点

	for (int i = 0; i < 7; i++) {
		int nr = r * (i + 1);  // 当前六边形的半径

		// 绘制当前六边形的所有点
		for (int j = 0; j < 6; j++) {
			//在此注意，sin输入的是弧度，需要*pi/180转化为角度输入
			int px = x + nr * sin(60 * j * constVal.PI / 180);  // 当前点的 x 坐标
			int py = y - nr * cos(60 * j * constVal.PI / 180);  // 当前点的 y 坐标

			putpixel(px, py, WHITE);
			hexagon[j].x = px;
			hexagon[j].y = py;
		}
		//连接六边形
		for (int j = 0; j < 6; j++) {
			if (j == 5) line(hexagon[j].x, hexagon[j].y, hexagon[0].x, hexagon[0].y);
			else line(hexagon[j].x, hexagon[j].y, hexagon[j + 1].x, hexagon[j + 1].y);
		}
		line(hexagon[0].x - (6 - i) * r * sin(60 * constVal.PI / 180),
			hexagon[0].y - (6 - i) * r * cos(60 * constVal.PI / 180),
			hexagon[1].x + (6 - i) * r * sin(60 * constVal.PI / 180),
			hexagon[1].y + (6 - i) * r * cos(60 * constVal.PI / 180)
		);

		line(hexagon[1].x - (6 - i) * r * sin(0 * constVal.PI / 180),
			hexagon[1].y - (6 - i) * r * cos(0 * constVal.PI / 180),
			hexagon[2].x + (6 - i) * r * sin(0 * constVal.PI / 180),
			hexagon[2].y + (6 - i) * r * cos(0 * constVal.PI / 180)
		);
		line(hexagon[3].x - (6 - i) * r * sin(60 * constVal.PI / 180),
			hexagon[3].y + (6 - i) * r * cos(60 * constVal.PI / 180),
			hexagon[2].x + (6 - i) * r * sin(60 * constVal.PI / 180),
			hexagon[2].y - (6 - i) * r * cos(60 * constVal.PI / 180)
		);
		line(hexagon[4].x - (6 - i) * r * sin(60 * constVal.PI / 180),
			hexagon[4].y - (6 - i) * r * cos(60 * constVal.PI / 180),
			hexagon[3].x + (6 - i) * r * sin(60 * constVal.PI / 180),
			hexagon[3].y + (6 - i) * r * cos(60 * constVal.PI / 180)
		);
		line(hexagon[5].x - (6 - i) * r * sin(0 * constVal.PI / 180),
			hexagon[5].y - (6 - i) * r * cos(0 * constVal.PI / 180),
			hexagon[4].x + (6 - i) * r * sin(0 * constVal.PI / 180),
			hexagon[4].y + (6 - i) * r * cos(0 * constVal.PI / 180)
		);
		line(hexagon[0].x + (6 - i) * r * sin(60 * constVal.PI / 180),
			hexagon[0].y - (6 - i) * r * cos(60 * constVal.PI / 180),
			hexagon[5].x - (6 - i) * r * sin(60 * constVal.PI / 180),
			hexagon[5].y + (6 - i) * r * cos(60 * constVal.PI / 180)
		);
	}
	//连接中心点与最大六边形
	setlinecolor(WHITE);
	for (int i = 0; i < 6; i++) {
		line(x, y, hexagon[i].x, hexagon[i].y);
	}
	int arr[7] = { 7,8,9,10,11,12,13 };
	int id = 0;
	for (int i = 7; i >= 1; i--)
	{
		int startx = x - i * r * sin(60 * constVal.PI / 180);
		int count = arr[7 - i];
		int side = r * cos(60 * constVal.PI / 180);
		int starty = y - count * side;
		int step = side * 2;
		for (int j = 0; j <= count; j++)
		{
			board[id].pix_x = startx;
			board[id].pix_y = starty + j * step;
			id++;
		}
	}
	int side = r * cos(60 * constVal.PI / 180);
	int starty = y - 14 * side;
	int step = side * 2;
	for (int i = 0; i <= 14; i++)
	{
		board[id].pix_x = x;
		board[id].pix_y = starty + i * step;
		id++;
	}
	int arr2[8] = { 0,13,12,11,10,9,8,7 };
	for (int i = 1; i <= 7; i++)
	{
		int startx = x + i * r * sin(60 * constVal.PI / 180);
		int side = r * cos(60 * constVal.PI / 180);
		int starty = y - arr2[i] * side;
		int step = side * 2;
		for (int j = 0; j <= arr2[i]; j++)
		{
			board[id].pix_x = startx;
			board[id].pix_y = starty + j * step;
			id++;
		}
	}
}

bool is_valid_move(int move) {
	if (move < 0 || move >= 169) {
		return false;
	}
	return board[move].status == 0;
}

int find_node_id(int line_x, int line_y) {
	for (int i = 0; i < 169; ++i) {
		if (board[i].Line_x == line_x && board[i].Line_y == line_y) {
			return i;
		}
	}
	return -1;
}

void victory_anime() {//五个黑子相连记为胜利

	int x = 30;
	while (1)
	{

		settextcolor(RGB(255, 0, 0));
		settextstyle(x, 0, _T("黑体"));
		outtextxy(mysettingVal.x / 2 - 80, mysettingVal.y / 2 - 40, _T("胜利！"));
		Sleep(10);
		x++;
		if (x == 80) {
			break;
		}
	}

}
void defeat_anime() {//五个白子相连记为失败
	int x = 30;
	while (1)
	{

		settextcolor(RGB(255, 255, 255));
		settextstyle(x, 0, _T("黑体"));
		outtextxy(mysettingVal.x / 2 - 80, mysettingVal.y / 2 - 40, _T("失败！"));
		Sleep(10);
		x++;
		if (x == 80) {
			break;
		}
	}
}

bool is_winning_move(int line_x, int line_y) {
	int id = find_node_id(line_x, line_y);
	if (id == -1) {
		return false;
	}
	int status = board[id].status;

	// Check x-lines
	if (line_x >= -7 && line_x <= 7) {
		int line_x_index = line_x + 7;
		for (int i = 0; i < 11; ++i) {
			if (lines[line_x_index][i] != nullptr && lines[line_x_index][i + 1] != nullptr && lines[line_x_index][i + 2] != nullptr && lines[line_x_index][i + 3] != nullptr && lines[line_x_index][i + 4] != nullptr &&
				lines[line_x_index][i]->status == status &&
				lines[line_x_index][i + 1]->status == status &&
				lines[line_x_index][i + 2]->status == status &&
				lines[line_x_index][i + 3]->status == status &&
				lines[line_x_index][i + 4]->status == status) {
				setlinecolor(RED);
				setlinestyle(PS_SOLID, 2);
				line(lines[line_x_index][i]->pix_x, lines[line_x_index][i]->pix_y, lines[line_x_index][i + 4]->pix_x, lines[line_x_index][i + 4]->pix_y);
				if (lines[line_x_index][i]->status == 1)//胜利
					victory_anime();
				else
					defeat_anime();
				return true;
			}
		}
	}

	// Check y-lines
	if (line_y >= -7 && line_y <= 7) {
		int line_y_index = line_y + 7 + 15;
		for (int i = 0; i < 11; ++i) {
			if (lines[line_y_index][i] != nullptr && lines[line_y_index][i + 1] != nullptr && lines[line_y_index][i + 2] != nullptr && lines[line_y_index][i + 3] != nullptr && lines[line_y_index][i + 4] != nullptr &&
				lines[line_y_index][i]->status == status &&
				lines[line_y_index][i + 1]->status == status &&
				lines[line_y_index][i + 2]->status == status &&
				lines[line_y_index][i + 3]->status == status &&
				lines[line_y_index][i + 4]->status == status) {
				setlinecolor(RED);
				setlinestyle(PS_SOLID, 2);
				line(lines[line_y_index][i]->pix_x, lines[line_y_index][i]->pix_y, lines[line_y_index][i + 4]->pix_x, lines[line_y_index][i + 4]->pix_y);
				if (lines[line_y_index][i]->status == 1)//胜利
					victory_anime();
				else
					defeat_anime();
				return true;
			}
		}
	}
	// Check z-lines
	int line_z = -line_x - line_y;
	if (line_z >= -7 && line_z <= 7) {
		int line_z_index = line_z + 7 + 30;
		for (int i = 0; i < 11; ++i) {
			if (lines[line_z_index][i] != nullptr && lines[line_z_index][i + 1] != nullptr && lines[line_z_index][i + 2] != nullptr && lines[line_z_index][i + 3] != nullptr && lines[line_z_index][i + 4] != nullptr &&
				lines[line_z_index][i]->status == status &&
				lines[line_z_index][i + 1]->status == status &&
				lines[line_z_index][i + 2]->status == status &&
				lines[line_z_index][i + 3]->status == status &&
				lines[line_z_index][i + 4]->status == status) {
				setlinecolor(RED);
				setlinestyle(PS_SOLID, 2);
				line(lines[line_z_index][i]->pix_x, lines[line_z_index][i]->pix_y, lines[line_z_index][i + 4]->pix_x, lines[line_z_index][i + 4]->pix_y);
				if (lines[line_z_index][i]->status == 1)//胜利
					victory_anime();
				else
					defeat_anime();
				return true;
			}
		}
	}
	return false;
}

int evaluate_move(int move, int player) {

	int m_score = 0;//作为该空位置的分数返回
	int node_id = board[move].id;//获取本棋子id
	int line_x_index = board[node_id].Line_x + 7;
	int line_y_index = board[node_id].Line_y + 7 + 15;
	int line_z_index = board[node_id].Line_z + 7 + 30;
	int line_x_x = board[node_id].Line_x + 7;
	int line_y_y = board[node_id].Line_y + 7;
	int line_z_z = board[node_id].Line_z + 7;
	int indexY = 0;
	if (line_x_x <= 7)
	{
		int startY = 7 - line_x_x;
		indexY = line_y_y - startY;
	}
	else
	{
		indexY = line_y_y;
	}

	int indexX = 0;
	if (line_z_z <= 7)
	{
		int startX = 7 - line_z_z;
		indexX = line_x_x - startX;
	}
	else
	{
		indexX = line_x_x;
	}

	int indexZ = 0;
	if (line_y_y <= 7)
	{
		indexZ = 14 - line_z_z;
	}
	else
	{
		int startZ = 21 - line_y_y;
		indexZ = startZ - line_z_z;
	}
	if (player % 2 == 0)
	{
		//遍历6个棋子 x_y
		for (int i = indexY - 5; i <= indexY; i++) {//起始位置
			int aa[6] = { 3,3,3,3,3,3 };//保存遍历结果(白棋
			int bb[6] = { 3,3,3,3,3,3 };//保存遍历结果(黑棋
			int a_i = 0;
			int b_i = 0;
			if (i >= 0)
			{
				if (lines[line_x_index][i] != nullptr && lines[line_x_index][i + 1] != nullptr && lines[line_x_index][i + 2] != nullptr && lines[line_x_index][i + 3] != nullptr && lines[line_x_index][i + 4] != nullptr && lines[line_x_index][i + 5] != nullptr) {

					for (int j = i; j <= i + 5; j++) {//遍历六个相邻棋子
						aa[a_i++] = lines[line_x_index][j]->status;
						bb[b_i++] = lines[line_x_index][j]->status;
					}
					if (is_equal(a4_2, aa, 6)) {
						m_score = max(a4_2_score, m_score);
					}
					if (is_equal(a42[0], aa, 6) || is_equal(a42[1], aa, 6) || is_equal(a42[2], aa, 6) || is_equal(a42[3], aa, 6) || is_equal(a42[4], aa, 6) || is_equal(a42[5], aa, 6) || is_equal(a42[6], aa, 6) || is_equal(a42[7], aa, 6)) {
						m_score = max(a42_score, m_score);
					}

					if (is_equal(a4_1, bb, 6)) {
						m_score = max(a4_1_score, m_score);
					}
					if (is_equal(a41[0], bb, 6) || is_equal(a41[1], bb, 6) || is_equal(a41[2], bb, 6) || is_equal(a41[3], bb, 6) || is_equal(a41[4], bb, 6) || is_equal(a41[5], bb, 6) || is_equal(a41[6], bb, 6) || is_equal(a41[7], bb, 6)) {
						m_score = max(a41_score, m_score);
					}
				}

				for (int a_i = 0; a_i < 6; a_i++) {
					aa[a_i] = 3;
				}
				a_i = 0;
				for (int b_i = 0; b_i < 6; b_i++) {
					aa[b_i] = 3;
				}
				b_i = 0;
			}
		}

		//遍历6个棋子 y_z
		for (int i = indexZ - 5; i <= indexZ; i++) {//起始位置
			int aa[6] = { 3,3,3,3,3,3 };//保存遍历结果(白棋
			int bb[6] = { 3,3,3,3,3,3 };//保存遍历结果(黑棋
			int a_i = 0;
			int b_i = 0;
			if (i >= 0)
			{
				if (lines[line_y_index][i] != nullptr && lines[line_y_index][i + 1] != nullptr && lines[line_y_index][i + 2] != nullptr && lines[line_y_index][i + 3] != nullptr && lines[line_y_index][i + 4] != nullptr && lines[line_y_index][i + 5] != nullptr) {
					for (int j = i; j <= i + 5; j++) {//遍历六个相邻棋子
						aa[a_i++] = lines[line_y_index][j]->status;
						bb[b_i++] = lines[line_y_index][j]->status;
					}
					if (is_equal(a4_2, aa, 6)) {
						m_score = max(a4_2_score, m_score);
					}
					if (is_equal(a42[0], aa, 6) || is_equal(a42[1], aa, 6) || is_equal(a42[2], aa, 6) || is_equal(a42[3], aa, 6) || is_equal(a42[4], aa, 6) || is_equal(a42[5], aa, 6) || is_equal(a42[6], aa, 6) || is_equal(a42[7], aa, 6)) {
						m_score = max(a42_score, m_score);
					}

					if (is_equal(a4_1, bb, 6)) {
						m_score = max(a4_1_score, m_score);
					}
					if (is_equal(a41[0], bb, 6) || is_equal(a41[1], bb, 6) || is_equal(a41[2], bb, 6) || is_equal(a41[3], bb, 6) || is_equal(a41[4], bb, 6) || is_equal(a41[5], bb, 6) || is_equal(a41[6], bb, 6) || is_equal(a41[7], bb, 6)) {
						m_score = max(a41_score, m_score);
					}
				}

				for (int a_i = 0; a_i < 6; a_i++) {
					aa[a_i] = 3;
				}
				a_i = 0;
				for (int b_i = 0; b_i < 6; b_i++) {
					aa[b_i] = 3;
				}
				b_i = 0;
			}

		}

		//遍历6个棋子 z_x
		for (int i = indexX - 5; i <= indexX; i++) {//起始位置
			int aa[6] = { 3,3,3,3,3,3 };//保存遍历结果(白棋
			int bb[6] = { 3,3,3,3,3,3 };//保存遍历结果(黑棋
			int a_i = 0;
			int b_i = 0;
			if (i >= 0)
			{
				if (lines[line_z_index][i] != nullptr && lines[line_z_index][i + 1] != nullptr && lines[line_z_index][i + 2] != nullptr && lines[line_z_index][i + 3] != nullptr && lines[line_z_index][i + 4] != nullptr && lines[line_z_index][i + 5] != nullptr) {
					for (int j = i; j <= i + 5; j++) {//遍历六个相邻棋子
						aa[a_i++] = lines[line_z_index][j]->status;
						bb[b_i++] = lines[line_z_index][j]->status;
					}
					if (is_equal(a4_2, aa, 6)) {
						m_score = max(a4_2_score, m_score);
					}
					if (is_equal(a42[0], aa, 6) || is_equal(a42[1], aa, 6) || is_equal(a42[2], aa, 6) || is_equal(a42[3], aa, 6) || is_equal(a42[4], aa, 6) || is_equal(a42[5], aa, 6) || is_equal(a42[6], aa, 6) || is_equal(a42[7], aa, 6)) {
						m_score = max(a42_score, m_score);
					}

					if (is_equal(a4_1, bb, 6)) {
						m_score = max(a4_1_score, m_score);
					}
					if (is_equal(a41[0], bb, 6) || is_equal(a41[1], bb, 6) || is_equal(a41[2], bb, 6) || is_equal(a41[3], bb, 6) || is_equal(a41[4], bb, 6) || is_equal(a41[5], bb, 6) || is_equal(a41[6], bb, 6) || is_equal(a41[7], bb, 6)) {
						m_score = max(a41_score, m_score);
					}
				}

				for (int a_i = 0; a_i < 6; a_i++) {
					aa[a_i] = 3;
				}
				a_i = 0;
				for (int b_i = 0; b_i < 6; b_i++) {
					aa[b_i] = 3;
				}
				b_i = 0;
			}

		}

		//遍历5个棋子 x_y
		for (int i = indexY - 4; i <= indexY; i++) {//起始位置
			int aa[5] = { 3,3,3,3,3 };//保存遍历结果(白棋
			int bb[5] = { 3,3,3,3,3 };//保存遍历结果(黑棋
			int a_i = 0;
			int b_i = 0;
			if (i >= 0)
			{
				if (lines[line_x_index][i] != nullptr && lines[line_x_index][i + 1] != nullptr && lines[line_x_index][i + 2] != nullptr && lines[line_x_index][i + 3] != nullptr && lines[line_x_index][i + 4] != nullptr) {
					for (int j = i; j <= i + 4; j++) {//遍历5个相邻棋子
						aa[a_i++] = lines[line_x_index][j]->status;
						bb[b_i++] = lines[line_x_index][j]->status;
					}
					if (is_equal(a3_2[0], aa, 5) || is_equal(a3_2[1], aa, 5) || is_equal(a3_2[2], aa, 5)) {
						if (is_equal(a3_2[1], aa, 5) || is_equal(a3_2[2], aa, 5))
						{
							m_score = max(a3_2_score + 20, m_score);
						}
						else
							m_score = max(a3_2_score, m_score);
					}
					if (is_equal(a32[0], aa, 5) || is_equal(a32[1], aa, 5) || is_equal(a32[2], aa, 5) || is_equal(a32[3], aa, 5) || is_equal(a32[4], aa, 5) || is_equal(a32[5], aa, 5)) {
						m_score = max(a32_score, m_score);
					}

					if (is_equal(a3_1[0], bb, 5) || is_equal(a3_1[1], bb, 5) || is_equal(a3_1[2], bb, 5)) {
						if (is_equal(a3_1[1], bb, 5) || is_equal(a3_1[2], bb, 5))
						{
							m_score = max(a3_1_score + 20, m_score);
						}
						else
							m_score = max(a3_1_score, m_score);
					}
					if (is_equal(a31[0], bb, 5) || is_equal(a31[1], bb, 5) || is_equal(a31[2], bb, 5) || is_equal(a31[3], bb, 5) || is_equal(a31[4], bb, 5) || is_equal(a31[5], bb, 5)) {
						m_score = max(a31_score, m_score);
					}
				}

				for (int a_i = 0; a_i < 5; a_i++) {
					aa[a_i] = 3;
				}
				a_i = 0;
				for (int b_i = 0; b_i < 5; b_i++) {
					aa[b_i] = 3;
				}
				b_i = 0;
			}

		}

		//遍历5个棋子 y_z
		for (int i = indexZ - 4; i <= indexZ; i++) {//起始位置
			int aa[5] = { 3,3,3,3,3 };//保存遍历结果(白棋
			int bb[5] = { 3,3,3,3,3 };//保存遍历结果(黑棋
			int a_i = 0;
			int b_i = 0;
			if (i >= 0)
			{
				if (lines[line_y_index][i] != nullptr && lines[line_y_index][i + 1] != nullptr && lines[line_y_index][i + 2] != nullptr && lines[line_y_index][i + 3] != nullptr && lines[line_y_index][i + 4] != nullptr) {
					for (int j = i; j <= i + 4; j++) {//遍历5个相邻棋子
						aa[a_i++] = lines[line_y_index][j]->status;
						bb[b_i++] = lines[line_y_index][j]->status;
					}
					if (is_equal(a3_2[0], aa, 5) || is_equal(a3_2[1], aa, 5) || is_equal(a3_2[2], aa, 5)) {
						if (is_equal(a3_2[1], aa, 5) || is_equal(a3_2[2], aa, 5))
						{
							m_score = max(a3_2_score + 20, m_score);
						}
						else
							m_score = max(a3_2_score, m_score);
					}
					if (is_equal(a32[0], aa, 5) || is_equal(a32[1], aa, 5) || is_equal(a32[2], aa, 5) || is_equal(a32[3], aa, 5) || is_equal(a32[4], aa, 5) || is_equal(a32[5], aa, 5)) {
						m_score = max(a32_score, m_score);
					}

					if (is_equal(a3_1[0], bb, 5) || is_equal(a3_1[1], bb, 5) || is_equal(a3_1[2], bb, 5)) {
						if (is_equal(a3_1[1], bb, 5) || is_equal(a3_1[2], bb, 5))
						{
							m_score = max(a3_1_score + 20, m_score);
						}
						else
							m_score = max(a3_1_score, m_score);
					}
					if (is_equal(a31[0], bb, 5) || is_equal(a31[1], bb, 5) || is_equal(a31[2], bb, 5) || is_equal(a31[3], bb, 5) || is_equal(a31[4], bb, 5) || is_equal(a31[5], bb, 5)) {
						m_score = max(a31_score, m_score);
					}
				}

				for (int a_i = 0; a_i < 5; a_i++) {
					aa[a_i] = 3;
				}
				a_i = 0;
				for (int b_i = 0; b_i < 5; b_i++) {
					aa[b_i] = 3;
				}
				b_i = 0;
			}

		}

		//遍历5个棋子 z_x
		for (int i = indexX - 4; i <= indexX; i++) {//起始位置
			int aa[5] = { 3,3,3,3,3 };//保存遍历结果(白棋
			int bb[5] = { 3,3,3,3,3 };//保存遍历结果(黑棋
			int a_i = 0;
			int b_i = 0;
			if (i >= 0)
			{
				if (lines[line_z_index][i] != nullptr && lines[line_z_index][i + 1] != nullptr && lines[line_z_index][i + 2] != nullptr && lines[line_z_index][i + 3] != nullptr && lines[line_z_index][i + 4] != nullptr) {
					for (int j = i; j <= i + 4; j++) {//遍历5个相邻棋子
						aa[a_i++] = lines[line_z_index][j]->status;
						bb[b_i++] = lines[line_z_index][j]->status;
					}
					if (is_equal(a3_2[0], aa, 5) || is_equal(a3_2[1], aa, 5) || is_equal(a3_2[2], aa, 5)) {
						if (is_equal(a3_2[1], aa, 5) || is_equal(a3_2[2], aa, 5))
						{
							m_score = max(a3_2_score + 20, m_score);
						}
						else
							m_score = max(a3_2_score, m_score);
					}
					if (is_equal(a32[0], aa, 5) || is_equal(a32[1], aa, 5) || is_equal(a32[2], aa, 5) || is_equal(a32[3], aa, 5) || is_equal(a32[4], aa, 5) || is_equal(a32[5], aa, 5)) {
						m_score = max(a32_score, m_score);
					}

					if (is_equal(a3_1[0], bb, 5) || is_equal(a3_1[1], bb, 5) || is_equal(a3_1[2], bb, 5)) {
						if (is_equal(a3_1[1], bb, 5) || is_equal(a3_1[2], bb, 5))
						{
							m_score = max(a3_1_score + 20, m_score);
						}
						else
							m_score = max(a3_1_score, m_score);
					}
					if (is_equal(a31[0], bb, 5) || is_equal(a31[1], bb, 5) || is_equal(a31[2], bb, 5) || is_equal(a31[3], bb, 5) || is_equal(a31[4], bb, 5) || is_equal(a31[5], bb, 5)) {
						m_score = max(a31_score, m_score);
					}
				}

				for (int a_i = 0; a_i < 5; a_i++) {
					aa[a_i] = 3;
				}
				a_i = 0;
				for (int b_i = 0; b_i < 5; b_i++) {
					aa[b_i] = 3;
				}
				b_i = 0;
			}

		}

		//遍历4个棋子 x_y
		for (int i = indexY - 3; i <= indexY; i++) {//起始位置
			int aa[4] = { 3,3,3,3 };//保存遍历结果(白棋
			int bb[4] = { 3,3,3,3 };//保存遍历结果(黑棋
			int a_i = 0;
			int b_i = 0;
			if (i >= 0)
			{
				if (lines[line_x_index][i] != nullptr && lines[line_x_index][i + 1] != nullptr && lines[line_x_index][i + 2] != nullptr && lines[line_x_index][i + 3] != nullptr) {
					for (int j = i; j <= i + 3; j++) {//遍历5个相邻棋子
						aa[a_i++] = lines[line_x_index][j]->status;
						bb[b_i++] = lines[line_x_index][j]->status;
					}
					if (is_equal(a2_2, aa, 4)) {
						m_score = max(a2_2_score, m_score);
					}
					if (is_equal(a22[0], aa, 4) || is_equal(a22[1], aa, 4) || is_equal(a22[2], aa, 4) || is_equal(a22[3], aa, 4)) {
						m_score = max(a22_score, m_score);
					}

					if (is_equal(a2_1, bb, 4)) {
						m_score = max(a2_1_score, m_score);
					}
					if (is_equal(a21[0], bb, 4) || is_equal(a21[1], bb, 4) || is_equal(a21[2], bb, 4) || is_equal(a21[3], bb, 4)) {
						m_score = max(a21_score, m_score);
					}
				}

				for (int a_i = 0; a_i < 4; a_i++) {
					aa[a_i] = 3;
				}
				a_i = 0;
				for (int b_i = 0; b_i < 4; b_i++) {
					aa[b_i] = 3;
				}
				b_i = 0;
			}

		}

		//遍历4个棋子 y_z
		for (int i = indexZ - 3; i <= indexZ; i++) {//起始位置
			int aa[4] = { 3,3,3,3 };//保存遍历结果(白棋
			int bb[4] = { 3,3,3,3 };//保存遍历结果(黑棋
			int a_i = 0;
			int b_i = 0;
			if (i >= 0)
			{
				if (lines[line_y_index][i] != nullptr && lines[line_y_index][i + 1] != nullptr && lines[line_y_index][i + 2] != nullptr && lines[line_y_index][i + 3] != nullptr) {
					for (int j = i; j <= i + 3; j++) {//遍历5个相邻棋子
						aa[a_i++] = lines[line_y_index][j]->status;
						bb[b_i++] = lines[line_y_index][j]->status;
					}
					if (is_equal(a2_2, aa, 4)) {
						m_score = max(a2_2_score, m_score);
					}
					if (is_equal(a22[0], aa, 4) || is_equal(a22[1], aa, 4) || is_equal(a22[2], aa, 4) || is_equal(a22[3], aa, 4)) {
						m_score = max(a22_score, m_score);
					}

					if (is_equal(a2_1, bb, 4)) {
						m_score = max(a2_1_score, m_score);
					}
					if (is_equal(a21[0], bb, 4) || is_equal(a21[1], bb, 4) || is_equal(a21[2], bb, 4) || is_equal(a21[3], bb, 4)) {
						m_score = max(a21_score, m_score);
					}
				}

				for (int a_i = 0; a_i < 4; a_i++) {
					aa[a_i] = 3;
				}
				a_i = 0;
				for (int b_i = 0; b_i < 4; b_i++) {
					aa[b_i] = 3;
				}
				b_i = 0;
			}

		}
		//遍历4个棋子 z_x
		for (int i = indexX - 3; i <= indexX; i++) {//起始位置
			int aa[4] = { 3,3,3,3 };//保存遍历结果(白棋
			int bb[4] = { 3,3,3,3 };//保存遍历结果(黑棋
			int a_i = 0;
			int b_i = 0;
			if (i >= 0)
			{
				if (lines[line_z_index][i] != nullptr && lines[line_z_index][i + 1] != nullptr && lines[line_z_index][i + 2] != nullptr && lines[line_z_index][i + 3] != nullptr) {
					for (int j = i; j <= i + 3; j++) {//遍历5个相邻棋子
						aa[a_i++] = lines[line_z_index][j]->status;
						bb[b_i++] = lines[line_z_index][j]->status;
					}
					if (is_equal(a2_2, aa, 4)) {
						m_score = max(a2_2_score, m_score);
					}
					if (is_equal(a22[0], aa, 4) || is_equal(a22[1], aa, 4) || is_equal(a22[2], aa, 4) || is_equal(a22[3], aa, 4)) {
						m_score = max(a22_score, m_score);
					}

					if (is_equal(a2_1, bb, 4)) {
						m_score = max(a2_1_score, m_score);
					}
					if (is_equal(a21[0], bb, 4) || is_equal(a21[1], bb, 4) || is_equal(a21[2], bb, 4) || is_equal(a21[3], bb, 4)) {
						m_score = max(a21_score, m_score);
					}
				}

				for (int a_i = 0; a_i < 4; a_i++) {
					aa[a_i] = 3;
				}
				a_i = 0;
				for (int b_i = 0; b_i < 4; b_i++) {
					aa[b_i] = 3;
				}
				b_i = 0;
			}

		}
	}
	else
	{
	//遍历6个棋子 x_y
	for (int i = indexY - 5; i <= indexY; i++) {//起始位置
		int aa[6] = { 3,3,3,3,3,3 };//保存遍历结果(白棋
		int bb[6] = { 3,3,3,3,3,3 };//保存遍历结果(黑棋
		int a_i = 0;
		int b_i = 0;
		if (i >= 0)
		{
			if (lines[line_x_index][i] != nullptr && lines[line_x_index][i + 1] != nullptr && lines[line_x_index][i + 2] != nullptr && lines[line_x_index][i + 3] != nullptr && lines[line_x_index][i + 4] != nullptr && lines[line_x_index][i + 5] != nullptr) {

				for (int j = i; j <= i + 5; j++) {//遍历六个相邻棋子
					aa[a_i++] = lines[line_x_index][j]->status;
					bb[b_i++] = lines[line_x_index][j]->status;
				}
				if (is_equal(a4_21, aa, 6)) {
					m_score = max(a4_2_score, m_score);
				}
				if (is_equal(a421[0], aa, 6) || is_equal(a421[1], aa, 6) || is_equal(a421[2], aa, 6) || is_equal(a421[3], aa, 6) || is_equal(a421[4], aa, 6) || is_equal(a421[5], aa, 6) || is_equal(a421[6], aa, 6) || is_equal(a421[7], aa, 6)) {
					m_score = max(a42_score, m_score);
				}

				if (is_equal(a4_11, bb, 6)) {
					m_score = max(a4_1_score, m_score);
				}
				if (is_equal(a411[0], bb, 6) || is_equal(a411[1], bb, 6) || is_equal(a411[2], bb, 6) || is_equal(a411[3], bb, 6) || is_equal(a411[4], bb, 6) || is_equal(a411[5], bb, 6) || is_equal(a411[6], bb, 6) || is_equal(a411[7], bb, 6)) {
					m_score = max(a41_score, m_score);
				}
			}

			for (int a_i = 0; a_i < 6; a_i++) {
				aa[a_i] = 3;
			}
			a_i = 0;
			for (int b_i = 0; b_i < 6; b_i++) {
				aa[b_i] = 3;
			}
			b_i = 0;
		}
	}

	//遍历6个棋子 y_z
	for (int i = indexZ - 5; i <= indexZ; i++) {//起始位置
		int aa[6] = { 3,3,3,3,3,3 };//保存遍历结果(白棋
		int bb[6] = { 3,3,3,3,3,3 };//保存遍历结果(黑棋
		int a_i = 0;
		int b_i = 0;
		if (i >= 0)
		{
			if (lines[line_y_index][i] != nullptr && lines[line_y_index][i + 1] != nullptr && lines[line_y_index][i + 2] != nullptr && lines[line_y_index][i + 3] != nullptr && lines[line_y_index][i + 4] != nullptr && lines[line_y_index][i + 5] != nullptr) {
				for (int j = i; j <= i + 5; j++) {//遍历六个相邻棋子
					aa[a_i++] = lines[line_y_index][j]->status;
					bb[b_i++] = lines[line_y_index][j]->status;
				}
				if (is_equal(a4_21, aa, 6)) {
					m_score = max(a4_2_score, m_score);
				}
				if (is_equal(a421[0], aa, 6) || is_equal(a421[1], aa, 6) || is_equal(a421[2], aa, 6) || is_equal(a421[3], aa, 6) || is_equal(a421[4], aa, 6) || is_equal(a421[5], aa, 6) || is_equal(a421[6], aa, 6) || is_equal(a421[7], aa, 6)) {
					m_score = max(a42_score, m_score);
				}

				if (is_equal(a4_11, bb, 6)) {
					m_score = max(a4_1_score, m_score);
				}
				if (is_equal(a411[0], bb, 6) || is_equal(a411[1], bb, 6) || is_equal(a411[2], bb, 6) || is_equal(a411[3], bb, 6) || is_equal(a411[4], bb, 6) || is_equal(a411[5], bb, 6) || is_equal(a411[6], bb, 6) || is_equal(a411[7], bb, 6)) {
					m_score = max(a41_score, m_score);
				}
			}

			for (int a_i = 0; a_i < 6; a_i++) {
				aa[a_i] = 3;
			}
			a_i = 0;
			for (int b_i = 0; b_i < 6; b_i++) {
				aa[b_i] = 3;
			}
			b_i = 0;
		}

	}

	//遍历6个棋子 z_x
	for (int i = indexX - 5; i <= indexX; i++) {//起始位置
		int aa[6] = { 3,3,3,3,3,3 };//保存遍历结果(白棋
		int bb[6] = { 3,3,3,3,3,3 };//保存遍历结果(黑棋
		int a_i = 0;
		int b_i = 0;
		if (i >= 0)
		{
			if (lines[line_z_index][i] != nullptr && lines[line_z_index][i + 1] != nullptr && lines[line_z_index][i + 2] != nullptr && lines[line_z_index][i + 3] != nullptr && lines[line_z_index][i + 4] != nullptr && lines[line_z_index][i + 5] != nullptr) {
				for (int j = i; j <= i + 5; j++) {//遍历六个相邻棋子
					aa[a_i++] = lines[line_z_index][j]->status;
					bb[b_i++] = lines[line_z_index][j]->status;
				}
				if (is_equal(a4_21, aa, 6)) {
					m_score = max(a4_2_score, m_score);
				}
				if (is_equal(a421[0], aa, 6) || is_equal(a421[1], aa, 6) || is_equal(a421[2], aa, 6) || is_equal(a421[3], aa, 6) || is_equal(a421[4], aa, 6) || is_equal(a421[5], aa, 6) || is_equal(a421[6], aa, 6) || is_equal(a421[7], aa, 6)) {
					m_score = max(a42_score, m_score);
				}

				if (is_equal(a4_11, bb, 6)) {
					m_score = max(a4_1_score, m_score);
				}
				if (is_equal(a411[0], bb, 6) || is_equal(a411[1], bb, 6) || is_equal(a411[2], bb, 6) || is_equal(a411[3], bb, 6) || is_equal(a411[4], bb, 6) || is_equal(a411[5], bb, 6) || is_equal(a411[6], bb, 6) || is_equal(a411[7], bb, 6)) {
					m_score = max(a41_score, m_score);
				}
			}

			for (int a_i = 0; a_i < 6; a_i++) {
				aa[a_i] = 3;
			}
			a_i = 0;
			for (int b_i = 0; b_i < 6; b_i++) {
				aa[b_i] = 3;
			}
			b_i = 0;
		}

	}

	//遍历5个棋子 x_y
	for (int i = indexY - 4; i <= indexY; i++) {//起始位置
		int aa[5] = { 3,3,3,3,3 };//保存遍历结果(白棋
		int bb[5] = { 3,3,3,3,3 };//保存遍历结果(黑棋
		int a_i = 0;
		int b_i = 0;
		if (i >= 0)
		{
			if (lines[line_x_index][i] != nullptr && lines[line_x_index][i + 1] != nullptr && lines[line_x_index][i + 2] != nullptr && lines[line_x_index][i + 3] != nullptr && lines[line_x_index][i + 4] != nullptr) {
				for (int j = i; j <= i + 4; j++) {//遍历5个相邻棋子
					aa[a_i++] = lines[line_x_index][j]->status;
					bb[b_i++] = lines[line_x_index][j]->status;
				}
				if (is_equal(a3_21[0], aa, 5) || is_equal(a3_21[1], aa, 5) || is_equal(a3_21[2], aa, 5)) {
					if (is_equal(a3_21[1], aa, 5) || is_equal(a3_21[2], aa, 5))
					{
						m_score = max(a3_2_score + 20, m_score);
					}
					else
						m_score = max(a3_2_score, m_score);
				}
				if (is_equal(a321[0], aa, 5) || is_equal(a321[1], aa, 5) || is_equal(a321[2], aa, 5) || is_equal(a321[3], aa, 5) || is_equal(a321[4], aa, 5) || is_equal(a321[5], aa, 5)) {
					m_score = max(a32_score, m_score);
				}

				if (is_equal(a3_11[0], bb, 5) || is_equal(a3_11[1], bb, 5) || is_equal(a3_11[2], bb, 5)) {
					if (is_equal(a3_11[1], bb, 5) || is_equal(a3_11[2], bb, 5))
					{
						m_score = max(a3_1_score + 20, m_score);
					}
					else
						m_score = max(a3_1_score, m_score);
				}
				if (is_equal(a311[0], bb, 5) || is_equal(a311[1], bb, 5) || is_equal(a311[2], bb, 5) || is_equal(a311[3], bb, 5) || is_equal(a311[4], bb, 5) || is_equal(a311[5], bb, 5)) {
					m_score = max(a31_score, m_score);
				}
			}

			for (int a_i = 0; a_i < 5; a_i++) {
				aa[a_i] = 3;
			}
			a_i = 0;
			for (int b_i = 0; b_i < 5; b_i++) {
				aa[b_i] = 3;
			}
			b_i = 0;
		}

	}

	//遍历5个棋子 y_z
	for (int i = indexZ - 4; i <= indexZ; i++) {//起始位置
		int aa[5] = { 3,3,3,3,3 };//保存遍历结果(白棋
		int bb[5] = { 3,3,3,3,3 };//保存遍历结果(黑棋
		int a_i = 0;
		int b_i = 0;
		if (i >= 0)
		{
			if (lines[line_y_index][i] != nullptr && lines[line_y_index][i + 1] != nullptr && lines[line_y_index][i + 2] != nullptr && lines[line_y_index][i + 3] != nullptr && lines[line_y_index][i + 4] != nullptr) {
				for (int j = i; j <= i + 4; j++) {//遍历5个相邻棋子
					aa[a_i++] = lines[line_y_index][j]->status;
					bb[b_i++] = lines[line_y_index][j]->status;
				}
				if (is_equal(a3_21[0], aa, 5) || is_equal(a3_21[1], aa, 5) || is_equal(a3_21[2], aa, 5)) {
					if (is_equal(a3_21[1], aa, 5) || is_equal(a3_21[2], aa, 5))
					{
						m_score = max(a3_2_score + 20, m_score);
					}
					else
						m_score = max(a3_2_score, m_score);
				}
				if (is_equal(a321[0], aa, 5) || is_equal(a321[1], aa, 5) || is_equal(a321[2], aa, 5) || is_equal(a321[3], aa, 5) || is_equal(a321[4], aa, 5) || is_equal(a321[5], aa, 5)) {
					m_score = max(a32_score, m_score);
				}

				if (is_equal(a3_11[0], bb, 5) || is_equal(a3_11[1], bb, 5) || is_equal(a3_11[2], bb, 5)) {
					if (is_equal(a3_11[1], bb, 5) || is_equal(a3_11[2], bb, 5))
					{
						m_score = max(a3_1_score + 20, m_score);
					}
					else
						m_score = max(a3_1_score, m_score);
				}
				if (is_equal(a311[0], bb, 5) || is_equal(a311[1], bb, 5) || is_equal(a311[2], bb, 5) || is_equal(a311[3], bb, 5) || is_equal(a311[4], bb, 5) || is_equal(a311[5], bb, 5)) {
					m_score = max(a31_score, m_score);
				}
			}

			for (int a_i = 0; a_i < 5; a_i++) {
				aa[a_i] = 3;
			}
			a_i = 0;
			for (int b_i = 0; b_i < 5; b_i++) {
				aa[b_i] = 3;
			}
			b_i = 0;
		}

	}

	//遍历5个棋子 z_x
	for (int i = indexX - 4; i <= indexX; i++) {//起始位置
		int aa[5] = { 3,3,3,3,3 };//保存遍历结果(白棋
		int bb[5] = { 3,3,3,3,3 };//保存遍历结果(黑棋
		int a_i = 0;
		int b_i = 0;
		if (i >= 0)
		{
			if (lines[line_z_index][i] != nullptr && lines[line_z_index][i + 1] != nullptr && lines[line_z_index][i + 2] != nullptr && lines[line_z_index][i + 3] != nullptr && lines[line_z_index][i + 4] != nullptr) {
				for (int j = i; j <= i + 4; j++) {//遍历5个相邻棋子
					aa[a_i++] = lines[line_z_index][j]->status;
					bb[b_i++] = lines[line_z_index][j]->status;
				}
				if (is_equal(a3_21[0], aa, 5) || is_equal(a3_21[1], aa, 5) || is_equal(a3_21[2], aa, 5)) {
					if (is_equal(a3_21[1], aa, 5) || is_equal(a3_21[2], aa, 5))
					{
						m_score = max(a3_2_score + 20, m_score);
					}
					else
						m_score = max(a3_2_score, m_score);
				}
				if (is_equal(a321[0], aa, 5) || is_equal(a321[1], aa, 5) || is_equal(a321[2], aa, 5) || is_equal(a321[3], aa, 5) || is_equal(a321[4], aa, 5) || is_equal(a321[5], aa, 5)) {
					m_score = max(a32_score, m_score);
				}

				if (is_equal(a3_11[0], bb, 5) || is_equal(a3_11[1], bb, 5) || is_equal(a3_11[2], bb, 5)) {
					if (is_equal(a3_11[1], bb, 5) || is_equal(a3_11[2], bb, 5))
					{
						m_score = max(a3_1_score + 20, m_score);
					}
					else
						m_score = max(a3_1_score, m_score);
				}
				if (is_equal(a311[0], bb, 5) || is_equal(a311[1], bb, 5) || is_equal(a311[2], bb, 5) || is_equal(a311[3], bb, 5) || is_equal(a311[4], bb, 5) || is_equal(a311[5], bb, 5)) {
					m_score = max(a31_score, m_score);
				}
			}

			for (int a_i = 0; a_i < 5; a_i++) {
				aa[a_i] = 3;
			}
			a_i = 0;
			for (int b_i = 0; b_i < 5; b_i++) {
				aa[b_i] = 3;
			}
			b_i = 0;
		}

	}

	//遍历4个棋子 x_y
	for (int i = indexY - 3; i <= indexY; i++) {//起始位置
		int aa[4] = { 3,3,3,3 };//保存遍历结果(白棋
		int bb[4] = { 3,3,3,3 };//保存遍历结果(黑棋
		int a_i = 0;
		int b_i = 0;
		if (i >= 0)
		{
			if (lines[line_x_index][i] != nullptr && lines[line_x_index][i + 1] != nullptr && lines[line_x_index][i + 2] != nullptr && lines[line_x_index][i + 3] != nullptr) {
				for (int j = i; j <= i + 3; j++) {//遍历5个相邻棋子
					aa[a_i++] = lines[line_x_index][j]->status;
					bb[b_i++] = lines[line_x_index][j]->status;
				}
				if (is_equal(a2_21, aa, 4)) {
					m_score = max(a2_2_score, m_score);
				}
				if (is_equal(a221[0], aa, 4) || is_equal(a221[1], aa, 4) || is_equal(a221[2], aa, 4) || is_equal(a221[3], aa, 4)) {
					m_score = max(a22_score, m_score);
				}

				if (is_equal(a2_11, bb, 4)) {
					m_score = max(a2_1_score, m_score);
				}
				if (is_equal(a211[0], bb, 4) || is_equal(a211[1], bb, 4) || is_equal(a211[2], bb, 4) || is_equal(a211[3], bb, 4)) {
					m_score = max(a21_score, m_score);
				}
			}

			for (int a_i = 0; a_i < 4; a_i++) {
				aa[a_i] = 3;
			}
			a_i = 0;
			for (int b_i = 0; b_i < 4; b_i++) {
				aa[b_i] = 3;
			}
			b_i = 0;
		}

	}

	//遍历4个棋子 y_z
	for (int i = indexZ - 3; i <= indexZ; i++) {//起始位置
		int aa[4] = { 3,3,3,3 };//保存遍历结果(白棋
		int bb[4] = { 3,3,3,3 };//保存遍历结果(黑棋
		int a_i = 0;
		int b_i = 0;
		if (i >= 0)
		{
			if (lines[line_y_index][i] != nullptr && lines[line_y_index][i + 1] != nullptr && lines[line_y_index][i + 2] != nullptr && lines[line_y_index][i + 3] != nullptr) {
				for (int j = i; j <= i + 3; j++) {//遍历5个相邻棋子
					aa[a_i++] = lines[line_y_index][j]->status;
					bb[b_i++] = lines[line_y_index][j]->status;
				}
				if (is_equal(a2_21, aa, 4)) {
					m_score = max(a2_2_score, m_score);
				}
				if (is_equal(a221[0], aa, 4) || is_equal(a221[1], aa, 4) || is_equal(a221[2], aa, 4) || is_equal(a221[3], aa, 4)) {
					m_score = max(a22_score, m_score);
				}

				if (is_equal(a2_11, bb, 4)) {
					m_score = max(a2_1_score, m_score);
				}
				if (is_equal(a211[0], bb, 4) || is_equal(a211[1], bb, 4) || is_equal(a211[2], bb, 4) || is_equal(a211[3], bb, 4)) {
					m_score = max(a21_score, m_score);
				}
			}

			for (int a_i = 0; a_i < 4; a_i++) {
				aa[a_i] = 3;
			}
			a_i = 0;
			for (int b_i = 0; b_i < 4; b_i++) {
				aa[b_i] = 3;
			}
			b_i = 0;
		}

	}
	//遍历4个棋子 z_x
	for (int i = indexX - 3; i <= indexX; i++) {//起始位置
		int aa[4] = { 3,3,3,3 };//保存遍历结果(白棋
		int bb[4] = { 3,3,3,3 };//保存遍历结果(黑棋
		int a_i = 0;
		int b_i = 0;
		if (i >= 0)
		{
			if (lines[line_z_index][i] != nullptr && lines[line_z_index][i + 1] != nullptr && lines[line_z_index][i + 2] != nullptr && lines[line_z_index][i + 3] != nullptr) {
				for (int j = i; j <= i + 3; j++) {//遍历5个相邻棋子
					aa[a_i++] = lines[line_z_index][j]->status;
					bb[b_i++] = lines[line_z_index][j]->status;
				}
				if (is_equal(a2_21, aa, 4)) {
					m_score = max(a2_2_score, m_score);
				}
				if (is_equal(a221[0], aa, 4) || is_equal(a221[1], aa, 4) || is_equal(a221[2], aa, 4) || is_equal(a221[3], aa, 4)) {
					m_score = max(a22_score, m_score);
				}

				if (is_equal(a2_11, bb, 4)) {
					m_score = max(a2_1_score, m_score);
				}
				if (is_equal(a211[0], bb, 4) || is_equal(a211[1], bb, 4) || is_equal(a211[2], bb, 4) || is_equal(a211[3], bb, 4)) {
					m_score = max(a21_score, m_score);
				}
			}

			for (int a_i = 0; a_i < 4; a_i++) {
				aa[a_i] = 3;
			}
			a_i = 0;
			for (int b_i = 0; b_i < 4; b_i++) {
				aa[b_i] = 3;
			}
			b_i = 0;
		}

	}
	 }
	return m_score;
}
bool compareDescending(const ScoreData& a, const ScoreData& b) {
	return a.score > b.score; // 按降序排序
}

void removeMove(int id)
{
	board[id].status = 0;
}
void setMove(int id,int player)
{
	board[id].status = player;
}
int GameStart() {
	ofstream file("scores.txt");
	int boardMargin = mysettingVal.sideLength * 2;
	int turn = 1;
	int xx1;
	int yy1;//保存玩家上一个棋子的落子位置
	int id1;
	int xx2;
	int yy2;//保存电脑上一个棋子的落子位置
	int id2;
	int flag = 0;//是否悔棋
	vector<ScoreData>scores;
	while (true) {
		if (turn % 2 == 1)
		{
			ExMessage m;        // 定义消息变量
			m = getmessage(EX_MOUSE | EX_KEY);

			switch (m.message) {
			case WM_MOUSEMOVE:
				break;

			case WM_LBUTTONDOWN:        // 左键被点击
			{
				// 检查鼠标点击是否在棋盘区域内
				if (m.x >= mysettingVal.midPointx - boardMargin * 7 && m.x <= mysettingVal.midPointx + boardMargin * 7 &&
					m.y >= mysettingVal.midPointy - boardMargin * 7 && m.y <= mysettingVal.midPointy + boardMargin * 7) {
					// 找到离鼠标点击最近的棋盘节点
					int closestNode = -1;
					int closestDistance = INT_MAX;

					for (int i = 0; i < 169; ++i) {
						int dx = board[i].pix_x - m.x;
						int dy = board[i].pix_y - m.y;
						int distance = dx * dx + dy * dy;

						if (distance < closestDistance) {
							closestDistance = distance;
							closestNode = i;
						}
					}

					if (is_valid_move(closestNode)) {
						// 创建一个新的棋子对象
						ChessPiece piece;
						piece.id = closestNode;
						id1 = piece.id;
						piece.status = 1;
						// 放置棋子
						int radius = 10;
						int x = board[closestNode].pix_x;
						xx1 = board[closestNode].pix_x;
						int y = board[closestNode].pix_y;
						yy1 = board[closestNode].pix_y;
						setfillcolor(BLACK);
						fillcircle(x, y, radius);

						// 更新棋盘状态
						board[closestNode].status = piece.status;
						if (is_winning_move(board[closestNode].Line_x, board[closestNode].Line_y))
						{

							for (int ii = 0; ii < 169; ii++) {
								cout << board[ii].status << ' ';
								cout << "分数：" << board[ii].score << ' ';
							}
							return 0;
						}
						// 打印放置棋子的信息
						cout << "Placed chess piece at node " << closestNode << " with status " << piece.status << endl;

						// 切换下一个玩家的回合
						turn++;
					}
				}
			}
			break;
			case WM_KEYDOWN:
				if (m.vkcode == VK_TAB) {//悔棋

					clearcircle(xx1, yy1, 10);
					flag = 1;
					board[id1].status = 0;
					Sleep(300);
					if (turn > 2 && flag == 1) {
						clearcircle(xx2, yy2, 10);
						flag = 0;
						board[id2].status = 0;
					}

				}
				if (m.vkcode == VK_ESCAPE)
					return 0;    // 按 ESC 键退出程序
			}
		}
		else
		{
			int bestMove = -1;
			long long bestScore = INT_MIN;
			if (turn > 2 && flag == 1) {
				clearcircle(xx2, yy2, 10);
				flag = 0;
				board[id2].status = 0;
			}
			for (int i = 0; i < 169; ++i) {
				if (is_valid_move(i)) {
					int s1 = board[i].score;
					int s2 = evaluate_move(i, 2);
					board[i].score = s1 + s2;
					scores.push_back({ i, board[i].score }); 
					if ((s1 + s2) > bestScore) {
						bestScore = s1 + s2;
						bestMove = i;
					}
				}
			}
			sort(scores.begin(), scores.end(), compareDescending);
			ScoreData first_element = scores[0];
			int threshold = first_element.score - 6;
			auto it = scores.begin();
			for (int i = 1; i < scores.size(); i++)
			{
				if (scores[i].score < threshold)
				{
					scores.erase(scores.begin() + i, scores.end());
				}
				file << "size = " << scores.size() << endl;
			}
			int bestScore1 = 1000000;
			for (int i = 0; i < scores.size(); i++)
			{
				setMove(scores[i].id, 2);
				for (int j = 0; j < 169; j++)
				{
					int s1 = evaluate_move(j, 1);
					int s2 = board[j].score;
					if ((s1 + s2) < bestScore1) {
						bestScore1 = s1 + s2;
						bestMove = scores[i].id;
					}
				}
				removeMove(scores[i].id);
			}
			file << "bestMove = "<<bestMove << endl;
			set_basic_score();
			scores.clear();
			// Place AI's chess piece on the best move
			if (bestMove != -1) {
				ChessPiece piece;
				piece.id = bestMove;
				id2 = piece.id;
				piece.status = 2;
				int radius = 10;
				int x = board[bestMove].pix_x;
				xx2 = board[bestMove].pix_x;
				int y = board[bestMove].pix_y;
				yy2 = board[bestMove].pix_y;
				Sleep(500);
				setfillcolor(WHITE);
				fillcircle(x, y, radius);
				board[bestMove].status = piece.status;
				if (is_winning_move(board[bestMove].Line_x, board[bestMove].Line_y)) {
					return 0;
				}
				turn++;
			}
		}
	}
}